/*
 ============================================================================
 Name        : fileLoader.c
 Author      :
 Version     :
 Copyright   : Your copyright notice
 Description : Load a binary input file
 ============================================================================
 */

#include "fileLoader.h"

/*! \fn openBinaryFile(char * fileName, int * fd)
 \brief Open a binary file to be read a dissasemble.
 \param fileName File name input.
 \param fd Pointer to input file.
 */
char openBinaryFile(char * fileName, int * fd)
{
	FILE *source;
	struct stat buf;
	unsigned long fileLen;

	PRINT_DEBUG(DEBUG_ALL, "Opening %s ...\n", fileName);

	if (stat(fileName, &buf) != 0)
	{
		PRINT_ERROR("Unable to open the file: %s", fileName);
		return (STATUS_ERROR);
	}

	source = fopen(fileName, "rb");
	fseek(source, 0, SEEK_END);
	fileLen = ftell(source);


	PRINT_DEBUG(DEBUG_ALL, "%lu", fileLen);

	if (fileLen >= BUFFER_SIZE)
	{
		printf("ERROR: Input file is bigger than 100mb.\n");
		return STATUS_ERROR;
	}
	fclose(source);

	*fd = open(fileName, O_RDONLY);

	if (*fd < 0)
	{
		PRINT_ERROR("Unable to open the file: %s", fileName);
		return STATUS_ERROR;
	}

	return STATUS_SUCCESS;
}

/*! \fn closeBinaryFile(FILE *pFile)
 \brief Close a binary file after work with it.
 \param pFile Analyzed file.
 */
char closeBinaryFile(FILE *pFile)
{
	fclose(pFile);
	return STATUS_SUCCESS;
}
